﻿Imports System.Data.SqlClient

Public Class DonVi
    Private iMaDv As Integer
    Private sTenDv As String
    Private bTrangThai As Boolean

    Public Property MaDv() As Integer
        Get
            Return iMaDv
        End Get
        Set(ByVal Value As Integer)
            iMaDv = Value
        End Set
    End Property

    Public Property TenDv() As String
        Get
            Return sTenDv
        End Get
        Set(ByVal Value As String)
            sTenDv = Value
        End Set
    End Property

    Public Property TrangThai() As Boolean
        Get
            Return bTrangThai
        End Get
        Set(ByVal Value As Boolean)
            bTrangThai = Value
        End Set
    End Property
End Class

Public Class DonViF

    Public Function GetAll() As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DonVi_GetAll"

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
        myCnn.Dispose()
    End Function

    Public Function GetWhereMaDv(ByVal MaDv As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DonVi_GetWhereMaDv"

            .Parameters.Add(New SqlClient.SqlParameter("@madv", SqlDbType.Char))
            .Parameters("@madv").Value = MaDv

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function ExistsMaDv(ByVal MaDv As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DonVi_ExistsMaDv"

            .Parameters.Add(New SqlClient.SqlParameter("@madv", SqlDbType.Char))
            .Parameters("@madv").Value = MaDv

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Insert(ByVal DonVi As DonVi) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DonVi_Insert"

            .Parameters.Add(New SqlClient.SqlParameter("@madv", SqlDbType.Char))
            .Parameters("@madv").Value = DonVi.MaDv

            .Parameters.Add(New SqlClient.SqlParameter("@tendv", SqlDbType.NVarChar))
            .Parameters("@tendv").Value = DonVi.TenDv

            .Parameters.Add(New SqlClient.SqlParameter("@trangthai", SqlDbType.Bit))
            .Parameters("@trangthai").Value = DonVi.TrangThai

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Update(ByVal DonVi As DonVi) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DonVi_Update"

            .Parameters.Add(New SqlClient.SqlParameter("@madv", SqlDbType.Char))
            .Parameters("@madv").Value = DonVi.MaDv

            .Parameters.Add(New SqlClient.SqlParameter("@tendv", SqlDbType.NVarChar))
            .Parameters("@tendv").Value = DonVi.TenDv

            .Parameters.Add(New SqlClient.SqlParameter("@trangthai", SqlDbType.Bit))
            .Parameters("@trangthai").Value = DonVi.TrangThai

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Delete(ByVal madv As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DonVi_Save"

            .Parameters.Add(New SqlClient.SqlParameter("@madv", SqlDbType.Char))
            .Parameters("@madv").Value = madv

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

End Class


